package cn.bxshey.resourceSharing.controller;


import cn.bxshey.resourceSharing.dto.login.LoginDto;
import cn.bxshey.resourceSharing.entity.R;
import cn.bxshey.resourceSharing.service.LoginService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @作者：不想说话而已
 * @Date：2023/12/18 10:53
 */

@RestController
public class LoginController {

    @Resource
    LoginService loginService;



    //登录
    @PostMapping("/login")
    public R<?> login(@RequestBody LoginDto loginDto){
        System.out.println(loginDto);
        Map<String,String> userInfo = new HashMap<>();
        userInfo.put("password",loginDto.getPassword());
        userInfo.put("auto", loginDto.getAuto());
        userInfo.put("captcha", loginDto.getCaptcha());
        userInfo.put("captchaId", loginDto.getCaptchaId());
        Map<String, Object> login = loginService.login(userInfo);
        if(Objects.isNull(login)){
            return R.error("登录失败");
        }
        return R.success(login);
    }

    //退出登录
    @GetMapping("/logout")
    public R<?> logout(@RequestHeader("AUTHORIZATION_TOKEN") String token){
        return R.success(loginService.logout());
    }


    //随机验证码
    @GetMapping("/captcha")
    public R<?> captcha(){
        return R.success(loginService.captcha());
    }

    //随机注册验证码
    @GetMapping("/captcha/enroll")
    public R<?> captchaEnroll(String email) throws MessagingException {
        loginService.captchaEnroll(email);
        return R.success();
    }
}
